<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" @click="contractFileUpload">
        合同文件上传
      </a-button>
      <!--<a-upload
        name="file"
        :multiple="false"
        :action="uploadAction"
        :headers="tokenHeader"
        :showUploadList="false"
        :beforeUpload="beforeUpload"
        @change="handleChange">

      </a-upload>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>

    <gh-contract-file-modal ref="GhContractFileModal" @ok="modalFormOk"></gh-contract-file-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import GhContractFileModal from "./modules/GhContractFileModal";

export default {
  name: 'GhContractFileList',
  mixins:[JeecgListMixin, mixinDevice],
  components: {
    GhContractFileModal,
  },
  data () {
    return {
      description: 'gh_contract_file管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          align:"center",
          customRender:function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        {
          title:'合同名称',
          align:"center",
          dataIndex: 'name'
        },
        {
          title:'文件大小',
          align:"center",
          dataIndex: 'size'
        },
        {
          title:'文件状态',
          align:"center",
          dataIndex: 'status',
          customRender:function (text) {
            if(text === 0){
              return "文件未上传"
            }else if(text === 1){
              return "文件上传中"
            }else if(text === 2){
              return "文件上传已完成"
            }else if(text === 3){
              return "文件上传失败"
            }else if(text === 4){
              return "文件等待转pdf"
            }else if(text === 5){
              return "文件已转换pdf"
            }else if(text === 6){
              return "加水印中"
            }else if(text === 7){
              return "加水印完毕"
            }else if(text === 8){
              return "文件转换中"
            }else {
              return "文件转换失败"
            }
          }
        },
        {
          title:'合同类型',
          align:"center",
          dataIndex: 'type',
          customRender:function (text) {
            if(text === 1){
              return "订金合同"
            }else {
              return "正式合同"
            }
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          fixed:"right",
          width:147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/ghContractFile/list",
        delete: "/ghContractFile/delete",
        deleteBatch: "/ghContractFile/deleteBatch",
        upload: "/ghContractFile/uploadContract"
      },
      dictOptions:{},
      superFieldList:[],
    }
  },
  computed: {
    uploadAction() {
      return window._CONFIG['domianURL'] + this.url.upload;
    }
  },
  methods: {
    contractFileUpload(){
      this.$refs.GhContractFileModal.visible = true;
    },
    beforeUpload(file) {
      var fileType = file.type;
      if (fileType === 'image') {
        if (fileType.indexOf('image') < 0) {
          this.$message.warning('请上传图片');
          return false;
        }
      } else if (fileType === 'file') {
        if (fileType.indexOf('image') >= 0) {
          this.$message.warning('请上传文件');
          return false;
        }
      }
      return true
    },
    handleChange(info) {
      if (info.file.status === 'done') {
        if (info.file.response.success) {
          this.loadData()
          this.$message.success(`${info.file.name} 上传成功!`);
        } else {
          this.$message.error(`${info.file.response.message}`);
        }
      } else if (info.file.status === 'error') {
        this.$message.error(`${info.file.response.message}`);
      }
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>